from inventory.models import Item,Location
from django.contrib import admin
from django.forms import ModelForm,ValidationError
import re

class ItemForm(ModelForm):
	class Meta:
		model = Item
	
	def clean_cas_num(self):
		cas = self.cleaned_data["cas_num"].replace(' ','')
		if not re.search(r'^[0-9]{1,6}-[0-9]{2}-[0-9]$', cas):
			raise ValidationError("The CAS # is not properly formatted.  Proper Formatting Example: 520-53-6")
		self.cleaned_data["cas_num"] = cas
		return self.cleaned_data["cas_num"]

class ItemAdmin(admin.ModelAdmin):
	form = ItemForm


admin.site.register(Item,ItemAdmin)
admin.site.register(Location)

